<?php 
/*
 module:		MBTI题目类型
 create_time:	2024-04-18 18:04:00
 author:		
 contact:		
*/

namespace app\admin\controller\Mbti;

use app\admin\service\Mbti\TopicTypeService;
use app\admin\model\Mbti\TopicType as TopicTypeModel;
use app\admin\controller\Admin;
use think\facade\Db;

class TopicType extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['mbti_id'] = $this->request->param('mbti_id', '', 'serach_in');

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'mt_id,type_name,sort,state,create_time,edit_time,mbti_id,subject_num';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'sort desc,mt_id desc';

			$res = TopicTypeService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}

	/*修改排序开关按钮操作*/
	function updateExt(){
		$postField = 'mt_id,sort,state';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(!$data['mt_id']) $this->error('参数错误');
		try{
			TopicTypeModel::update($data);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*箭头排序*/
	function arrowsort(){
		$postField = 'mt_id,sortid,type';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(empty($data['sortid'])){
			$this->error('操作失败，当前数据没有排序号');
		}
		if($data['type'] == 1){
			$where['sort'] = ['>',$data['sortid']];
			$info = TopicTypeModel::where(formatWhere($where))->order('sort asc')->find();
		}else{
			$where['sort'] = ['<',$data['sortid']];
			$info = TopicTypeModel::where(formatWhere($where))->order('sort desc')->find();
		}
		if(empty($info['sort'])){
			$this->error('操作失败，目标位置没有排序号');
		}
		if($info){
			try{
				TopicTypeModel::update(['mt_id'=>$data['mt_id'],'sort'=>$info['sort']]);
				TopicTypeModel::update(['mt_id'=>$info['mt_id'],'sort'=>$data['sortid']]);
			}catch(\Exception $e){
				throw new \think\exception\ValidateException ($e->getMessage());
			}
		}else{
			$this->error('目标位置没有数据');
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}
    /**start**/
	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return view('add');
		}else{
			$postField = 'mbti_id,type_name,state,create_time';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = TopicTypeService::add($data);
			if($res && empty($data['sort'])){
				TopicTypeModel::update(['sort'=>$res,'mt_id'=>$res]);
			}
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}
    /**end**/
	/*修改*/
	function edit(){
		if (!$this->request->isPost()){
			$mt_id = $this->request->get('mt_id','','serach_in');
			if(!$mt_id) $this->error('参数错误');
			$this->view->assign('info',checkData(TopicTypeModel::find($mt_id)));
			return view('edit');
		}else{
			$postField = 'mt_id,type_name,state,edit_time';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = TopicTypeService::edit($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

 /**start**/
	/*删除*/
	function del(){
		$idx =  $this->request->post('mt_id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
            //查询是否有题目
            $count = Db::name("mbti_subject")->where(['mt_id'=>explode(',',$idx)])->count();
            if($count){
                throw new ValidateException("请先删除所有题目");
            }
			TopicTypeModel::destroy(['mt_id'=>explode(',',$idx)],true);

		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}
    /**end**/



}

